//
//  HomePageC.m
//  UniverseProject
//
//  Created by king on 2021/6/15.
//  Copyright © 2021 Cloud2209. All rights reserved.
//

#import "UINavigationController+FDFullscreenPopGesture.h"
#import "CWJumpC.h"
#import "MBProgressHUD+LG.h"
#import "DHAuthorizationManager.h"
#import "MapViewC.h"
#import "DHGuidePageHUD.h"
#import "LGScrollTextViewC.h"
#import "BONaviViewController.h"
#import "DemoC1.h"
#import "UIViewController+CWLateralSlide.h"
#import "HomePageC.h"

@interface HomePageC ()

// 强引用
@property (nonatomic, strong) DHAuthorizationManager *authorizationManager;

@end

@implementation HomePageC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置APP引导页
    if ([NSString isBlankString:[LGCache getStringRecordByKey:APP_FIRST_START]]) {
        [LGCache saveStringRecord:APP_FIRST_START ByKey:APP_FIRST_START];
        [self setDynamicGuidePage];
    }
    
    //首页导航栏隐藏
    self.fd_prefersNavigationBarHidden = YES;
    
    // 授权管理初始化
    self.authorizationManager = [[DHAuthorizationManager alloc] init];
    // 请求权限校验
    DHAuthorizationKey key = DHAuthorizationKeyCamera;
    [self.authorizationManager checkAuthorizationForKey:key withParameters:nil completion:^(NSDictionary <NSNumber *, NSNumber *> *_Nonnull result) {
      // 授权结果result <@(DHAuthorizationKey), @(DHAuthorizationStatus)>
      // do something...
//        NSLog(@"%@",result);
    }];
    
    
    UIScrollView *sc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, K_SCREEN_HEIGHT)];
    sc.contentSize = CGSizeMake(K_SCREEN_WIDTH, K_SCREEN_HEIGHT * 2);
    [self.view addSubview:sc];
    self.loadLGNavgationBar = YES;
    
    UIButton *leftItem = [UIButton buttonWithTitle:@"左边抽屉" titleColor:LGCOLOR(@"000000") font:LGFONT(14) target:self action:@selector(navigationLeftItemClick)];
    leftItem.frame = CGRectMake(0, k_StatusBarHeight, 100, 44);
    [self.lg_navigationBar addSubview:leftItem];

    UIButton *rightItem = [UIButton buttonWithTitle:@"右边抽屉" titleColor:LGCOLOR(@"000000") font:LGFONT(14) target:self action:@selector(navigationRightItemClick)];
    rightItem.frame = CGRectMake(K_SCREEN_WIDTH - 100, k_StatusBarHeight, 100, 44);
    [self.lg_navigationBar addSubview:rightItem];
    
    
    NSInteger col = 3;
    CGFloat startX = 0;
    CGFloat startY = 0;
    CGFloat padding = 15;
    CGFloat itemWH = (K_SCREEN_WIDTH - padding * (col + 1)) / col;
    
    NSArray *funcArr = @[@"Map",@"首页",@"跑马灯",@"底部弹窗",@"indicator",@"缩放animation",@"分段选择",
    @"个人中心",@"数据持久",@"轻提示",@"富文本加载",@"loadingGif",@"常规push",@"文字自适应",@"表格高度自适应",@"请求",@"定时器",@"暂停",@"继续",@"调试"];
    for (int i = 0; i < funcArr.count; i ++) {
        startX = i % col * itemWH + padding * (i  % col + 1);
        startY = (i / col) * itemWH + padding * (i / col + 1) + k_SafeAreaTopHeight;
        UILabel *boxView = [[UILabel alloc] initWithFrame:CGRectMake(startX, startY, itemWH, itemWH)];
        boxView.text = funcArr[i];
        boxView.textAlignment = 1;
        boxView.tag = i;
        boxView.backgroundColor = RandomColor;
        [boxView addTarget:self action:@selector(tapClick:)];
        [sc addSubview:boxView];
    }
    
        UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
        left.direction=UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:left];
        
        UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
        right.direction=UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:right];

}
 
//- (BOOL)canBecomeFirstResponder {
//    return YES;
//}

#pragma mark --支持摇动--
//- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
//{
//    NSLog(@"摇动开始");
//    [[FLEXManager sharedManager] showExplorer];
//}

#pragma mark - 摇动结束

- (void)navigationLeftItemClick{
    [self cw_showDrawerViewController:[DemoC1 new] animationType:CWDrawerAnimationTypeMask configuration:nil];
}

- (void)navigationRightItemClick{
    
    LGBaseViewController *vc = [[CWJumpC alloc] init];
    BONaviViewController *nvc = [[BONaviViewController alloc] initWithRootViewController:vc];
    CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration defaultConfiguration];
    conf.direction = CWDrawerTransitionFromRight;
    conf.showAnimDuration = 0.25f;
    [self cw_showDrawerViewController:nvc animationType:CWDrawerAnimationTypeMask configuration:conf];
    
}


- (void)popUP{
    UIView *boxView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 300)];
    boxView.backgroundColor = LGCOLOR(@"000000");
    [boxView setCornerOnTop:10];
    [LGPopWindow showWindowBottomUp:boxView];
}



- (void)scan{
    Class PushClass = NSClassFromString(@"DemoPageController");
    PushVC([PushClass new]);
}


- (void)scan1{
    Class PushClass = NSClassFromString(@"LGScrollTextViewC");
    PushVC([PushClass new]);
}



- (void)setDynamicGuidePage {
//    NSArray *imageNameArray = @[@"guideImage6.gif",@"guideImage7.gif",@"guideImage8.gif"];
//    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame imageNameArray:imageNameArray buttonIsHidden:YES];
//    guidePage.slideInto = YES;
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TestVideo" ofType:@"mp4"];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    DHGuidePageHUD *guidePage = [[DHGuidePageHUD alloc] dh_initWithFrame:self.view.frame videoURL:url];
    [self.navigationController.view addSubview:guidePage];
}


- (void)tapClick:(UITapGestureRecognizer *)ges{
    
    switch (ges.view.tag) {
        case 0:
        {
            MapViewC *vc = [MapViewC new];
            PushVC(vc);
        }
            break;
        case 1:{
            [self scan];
        }
            break;
        case 2:{
            [self scan1];
        }
            break;
        case 3:{
            [self popUP];
        }
            break;
        case 4:{
            [ges.view showIndicator];
        }
            break;
        case 5:{
            Class PushClass = NSClassFromString(@"AnimationViewController");
            PushVC([PushClass new]);
        }
            break;
            
        case 6:{
            Class PushClass = NSClassFromString(@"CenterPageC");
            PushVC([PushClass new]);
        }
            break;
        case 7:{
            Class PushClass = NSClassFromString(@"PagerViewController");
            PushVC([PushClass new]);
        }
            break;
        case 8:{
            Class PushClass = NSClassFromString(@"EGOViewController");
            PushVC([PushClass new]);
        }
            break;
        case 9:{
            Class PushClass = NSClassFromString(@"MBToastC");
            PushVC([PushClass new]);
        }
            break;;
        case 10:{
            Class PushClass = NSClassFromString(@"FWBC");
            PushVC([PushClass new]);
        }
            break;
        case 11:{
            [MBProgressHUD showloadingGif];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //n秒后追加任务代码到主队列，并开始执行
                //打印当前线程
//                NSLog(@"after---%@",[NSThread currentThread]);
                [MBProgressHUD hideLoadingGif];
            });
        }
            break;
        case 12:{
            Class PushClass = NSClassFromString(@"CWJumpC");
            PushVC([PushClass new]);
        }
            break;
        case 13:{
            Class PushClass = NSClassFromString(@"SizeFitViewController");
            PushVC([PushClass new]);
        }
            break;;
        case 14:{
            Class PushClass = NSClassFromString(@"TableSizeFitViewController");
            PushVC([PushClass new]);
        }
            break;
        case 15:{
            [self testDataRequestSuccess:^{
                
            }];
        }
            break;
        case 16:{
            [User timerToRefreshToken];
        }
            break;
        case 17:{
            [User pauseTokenTimer];
        }
            break;
        case 18:{
            [User continueTimer];
        }
            break;
        case 19:{
            // Objective-C
//            [[FLEXManager sharedManager] showExplorer];
        }
            break;
            
        default:
            
            break;
    }
    
}


-(void)handleSwipes:(UISwipeGestureRecognizer *)recognizer
{
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft){
    }
    
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
//        NSLog(@"向右边滑动了!!!!!!");
        [self navigationLeftItemClick];
    }
}


- (void)testDataRequestSuccess:(void (^)(void))success{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *request_url = @"/U/AC/LOCAL/V1/REGISTER";
    params[@"ACB501"] = @13812345678;
    params[@"UCC003"] = @"123456";
    params[@"UCC007"] = @"123456";
    params[@"sms"] = @"123456";
    params[@"sms_code"] = @"123456";
    params[@"UCE385_GL"] = @"123456";
    params[@"ROLE"] = @"123456";
    params[NEED_TOKEN] = @(YES);
    [MBProgressHUD showActivityInView];
    [LGAFNetWork POST:request_url params:params success:^(NSURLSessionDataTask *task, ReqSuccessInfo *data) {
        NSLog(@"request_url-%@",data);
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInView:data.Error];
    } fail:^(NSURLSessionDataTask *task, ReqErrorInfo *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showTipMessageInView:error.Error];
    }];
}





@end
